package com.ruoyi.web.controller.system;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.Agent;
import com.ruoyi.system.domain.AgentRelationship;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.IAgentRelationshipService;
import com.ruoyi.system.service.IAgentService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

import java.util.Date;
import java.util.List;

/**
 * 公客管理Controller
 * 
 * @author ruoyi
 * @date 2022-04-16
 */
@Controller
@RequestMapping("/system/guest")
public class GuestController extends BaseController
{
    private String prefix = "system/guest";

    @Autowired
    private IAgentService agentService;
    @Autowired
    private IAgentRelationshipService agentRelationshipService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDictDataService sysDictDataService;

    @RequiresPermissions("system:guest:view")
    @GetMapping()
    public String guest()
    {
        return prefix + "/guest";
    }

    /**
     * 查询公客管理列表
     */
    @RequiresPermissions("system:guest:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Agent guest)
    {
        startPage();
        guest.setGuest("2");
        guest.setDeptId(ShiroUtils.getSysUser().getDeptId());
        List<Agent> list = agentService.selectGuestList(guest);
        for(Agent ag:list){
            SysUser sysUser = sysUserService.selectUserById(ag.getUserId());
            if(sysUser!=null){
                ag.setUserName(sysUser.getUserName());
            }
            AgentRelationship agentRelationship = new AgentRelationship();
            agentRelationship.setAgentId(ag.getId());
            List<AgentRelationship> agentRelationships = agentRelationshipService.selectAgentRelationshipList(agentRelationship);
            if(agentRelationships.size()>0){
                String rinfo = sysDictDataService.selectDictLabel("relationship_info", agentRelationships.get(agentRelationships.size()-1).getRelationshipInfo());
                String rcont = agentRelationships.get(agentRelationships.size()-1).getRelationshipResult();
                if(StringUtils.isNotBlank(rcont)){
                    rinfo = rinfo+"-"+rcont;
                }
                ag.setRecord(rinfo+"("+ DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",(Date)(agentRelationships.get(agentRelationships.size()-1).getCreateTime()))+")");
            }
        }
        return getDataTable(list);
    }

    /**
     * 修改公客
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Agent guest = agentService.selectAgentById(id);
        guest.setUserId(ShiroUtils.getUserId());
        guest.setUserName(ShiroUtils.getSysUser().getUserName());
        guest.setDeptId(sysUserService.selectUserById(ShiroUtils.getUserId()).getDeptId());
        guest.setGuest("1");
        mmap.put("guest", guest);
        return prefix + "/edit";
    }

    /**
     * 修改保存公客
     */
    @RequiresPermissions("system:guest:edit")
    @Log(title = "公客管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Agent guest)
    {
        Agent agentOld = agentService.selectAgentById(guest.getId());
        Agent agentParam = new Agent();
        agentParam.setTelephone(guest.getTelephone());
        List<Agent> agents = agentService.selectAgents(agentParam);
        if(!agentOld.getTelephone().equals(guest.getTelephone())&&agents.size()>0){
            return AjaxResult.error("手机号已存在！");
        }
        if(agentOld.getGuest().equals("1")){
            return AjaxResult.error("该中介已不是公客，请刷新后选择其他公客！");
        }
        return toAjax(agentService.updateAgent(guest));
    }

    /**
     * 加载系统用户
     */
    @GetMapping("/getSysUserList")
    @ResponseBody
    public List<SysUser> getSysUserList()
    {
        SysUser sysUser = new SysUser();
        List<SysUser> sysUserList = sysUserService.selectUserList(sysUser);
        return sysUserList;
    }


}
